package com.example.mode;

import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;

import com.blankj.utilcode.util.LogUtils;
import com.example.mode.adapter.MainAdapter;
import com.example.mode.adapter.ViewPager2Adapter;
import com.example.mode.databinding.ActivityMainBinding;
import com.example.mode.entity.DEntity;
import com.example.mode.holder.BannerHolder;
import com.example.mode.ui.fragment.Fragment1;
import com.example.mode.ui.fragment.Fragment2;
import com.example.mode.ui.fragment.Fragment3;
import com.google.android.material.tabs.TabLayout;
import com.utils.conmponentbase.app.BaseApplication;
import com.utils.conmponentbase.base.activity.BaseListActivity;
import com.utils.conmponentbase.base.viewmodel.BaseViewModel;
import com.utils.conmponentbase.base.viewpager2.TabLayoutMediator;
import com.zhpan.bannerview.BannerViewPager;
import com.zhpan.bannerview.constants.PageStyle;
import com.zhpan.bannerview.utils.BannerUtils;

import java.util.ArrayList;
import java.util.List;

import es.dmoral.toasty.Toasty;

public class MainActivity extends BaseListActivity<DEntity, ActivityMainBinding, BaseViewModel> {
    private BannerViewPager<String, BannerHolder> mBannerViewPager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_main;
    }

    @Override
    public void initData() {
        //ActivityMainBinding mainBinding = (ActivityMainBinding) mDataBinding;
        //mDataBinding.toolbar.setNavigationIcon(R.drawable.picture_icon_back);
        mDataBinding.toolbar.setListener((v, action, extra) -> finish());
        MainAdapter adapter = new MainAdapter(getTestData());
        mDataBinding.rvList.setAdapter(adapter);
        super.mRefreshLayout = mDataBinding.srfLayout;
        super.mQuickAdapter = adapter;
        super.initData();
        //BaseApplication.mPreferences.setUserid("12123");
        mBannerViewPager = mDataBinding.banner;
        mBannerViewPager.setHolderCreator(BannerHolder::new)
                .setOnPageClickListener(position -> {
                    Toasty.info(mContext, DataResourceUtil.getInstance().getImages().get(position)).show();
                })
                .setPageStyle(PageStyle.MULTI_PAGE)
                .setRevealWidth(BannerUtils.dp2px(8))
                .setPageMargin(BannerUtils.dp2px(8))
                .create(DataResourceUtil.getInstance().getImages());

        List<Fragment> fragments = new ArrayList<>();
        fragments.add(new Fragment1());
        fragments.add(new Fragment2());
        fragments.add(new Fragment3());
        String[] title = {"Fragment1", "Fragment2", "Fragment3"};
        //viewpager
        ViewPager2Adapter pager2Adapter = new ViewPager2Adapter(getSupportFragmentManager(), getLifecycle(), fragments);
        mDataBinding.viewPager2.setAdapter(pager2Adapter);
        new TabLayoutMediator(mDataBinding.tabLayout, mDataBinding.viewPager2, true, new TabLayoutMediator.OnConfigureTabCallback() {
            @Override
            public void onConfigureTab(@NonNull TabLayout.Tab tab, int position) {
                tab.setText(title[position]);
            }
        }).attach();
    }

    @Override
    public void loadListData(boolean isRefresh, Object layout, int page, int pageSize, LoadDataCallBack<DEntity> pCallBack) {
        pCallBack.loadCallback(getTestData());
    }

    private List<DEntity> getTestData() {
        LogUtils.e(BaseApplication.mPreferences.getUserid(""));
        List<DEntity> dEntities = new ArrayList<>();
        for (int a = 0; a < 11; a++) {
            DEntity dEntity = new DEntity();
            dEntity.image = DataResourceUtil.getInstance().getImage();
            dEntity.title = DataResourceUtil.getInstance().getTitle();
            dEntities.add(dEntity);
            if (page == 2 && a == 8) return dEntities;
        }
        return dEntities;
    }
}
